<%@ page errorPage="../error.jsp" %>
<%@ page import = "java.io.*" %>
<%@ page import = "java.net.*" %>
<%@ page import = "java.text.*" %>
<%@ page import = "java.util.*" %>
<%@ page import = "java.net.URLEncoder" %>
<%@ page import="com.mbbmap.app.dao.*" %>
<%@ page import="com.mbbmap.util.*" %>
<%@ page import="com.mbbmap.app.manager.*" %>
<%@ page import="com.mbbmap.app.home.*" %>
<jsp:useBean id="user" class="com.mbbmap.app.dao.UserDao" scope="request" />

<%
		//////System.out.println("srchUsersList.jsp Start");
        // get message if any
        String actionMsg = (String) request.getAttribute(Constants.ACTION_MSG_KEY);
        if (actionMsg == null)
    	    actionMsg="";
	   // Role List
	   ArrayList arlSecGroupList = new ArrayList();
	   arlSecGroupList = (ArrayList) request.getAttribute(Constants.SECURITY_UGROUP);

	   // Agency List
	   ArrayList arlAgencyList = (ArrayList) request.getAttribute(Constants.AGENCY_ARRAYLIST_OBJ);

	   ArrayList users = (ArrayList)request.getAttribute(Constants.USER_MAINTENANCE_LIST);

		int size = users.size();
		UserDao userD=null;
		////System.out.println("srchUsersList.jsp Start 01 arlSecGroupList.size()=" + arlSecGroupList.size());


	// Get the encryption key
	String encryKey = (String) session.getAttribute(Constants.ENCRYPTION_PASSPHRASE_KEY);

	// Logon user
	//SecUsersHome suh = SecUsersHome.getInstance();
	String logonUserId = (String) session.getAttribute(Constants.LOGINID);
	String merchantId = (String) session.getAttribute(Constants.MERCHANT_KEY);

	if (logonUserId == null || logonUserId.equals("") || merchantId == null || merchantId.equals("") ) {
		//////System.out.println("null");
		request.getRequestDispatcher("/LogOut.do?dispatch=sessionExp").forward(request, response);
		return;
	}

		////System.out.println("srchUsersList.jsp Start 04");

%>

<%@ include file="../top.jsp" %>

<SCRIPT LANGUAGE="JavaScript1.2">
	function getUsers(){

		var groupcdsel = document.GetUserForm.userRole[document.GetUserForm.userRole.selectedIndex].value;
		if(groupcdsel==null || groupcdsel==""){
			alert("Please select a group");
			return false;
		}

		var agncdsel = document.GetUserForm.agencyCode[document.GetUserForm.agencyCode.selectedIndex].value;
		if(agncdsel==null || agncdsel==""){
			alert("Please select an agency");
			return false;
		}

		var bar = agncdsel.split("_");
		var agncode = bar[0];
		var agncountrycd = bar[1];

		document.GetUserForm.usrRoleCd.value=groupcdsel;
		document.GetUserForm.usrCountryCd.value=agncountrycd;
		document.GetUserForm.usrAgencyCd.value=agncode;

		document.GetUserForm.action='<%=response.encodeURL("UserSearch.do?dispatch=srcUserList")%>';
		document.GetUserForm.submit();
	}
</SCRIPT>
<body>
		<table align="center" border=1 width = 70%>
			<tr>
				<th class="pageTitle" colspan="2">Users Maintenance</th>
			</tr>
			<tr>
				<th class="header mbb_blue" colspan="2" align="left">Search User List</th>
			</tr>
			<%
			if(actionMsg != null)
			{
				if(!actionMsg.equals(""))
				{
			%>
				<th class="pageTitle" colspan="2"><%=actionMsg%></th>
			<%
				}
			}
			%>
			<tr class="header">
				<td colspan = "2"></td>
			</tr>
  		</table>

	<form name="GetUserForm" action="" method="post">
		<input type="hidden" name = "usrRoleCd" value="">
		<input type="hidden" name = "usrAgencyCd" value="">
		<input type="hidden" name = "usrCountryCd" value="">
		<table align="center" class="tabularData" width=70% border="0" rules="cols">
			<thead class="th1">
				<tr>
					<th colspan="2">
						<div class="sectionTableTitle">
							<h2>Select Users By :</h2>
						</div>
					</th>
				</tr>
			</thead>
			<tr class="headerD" style="border: thin solid">
				<td width="50%" class="center_note_left">Select User Group</td>
				<td width="50%" class="center_note_left">
			   <select size="1" name="userRole">
					<option value="">--Please Select--</option>
				  <%
					////System.out.println("serachUser.jsp Start 05");
					 String selected="";
					 SecGroupLevelDao role;
					 for (int i=0; i < arlSecGroupList.size(); i++) {
						 role = (SecGroupLevelDao) arlSecGroupList.get(i);
						 if (user.getUserRole().equalsIgnoreCase(role.getcGroupCode())){
							 selected="selected";
						 }
				  %>

							 <option value="<%=role.getcGroupCode()%>" <%=selected%>> <%=role.getChildGroupName()%></option>
					  <%
							selected="";
						 }
					  %>
			   </select>
				</td>
			</tr>
			<tr class="headerD" style="border: thin solid">
				<td width="50%" class="center_note_left">Select User Agency</td>
				<td width="50%" class="center_note_left">
				   <select size="1" name="agencyCode">
				   	<option value="">--Please Select--</option>
				      <%
	      				 selected="";
	      				 AgencyDao agency;
						 for (int i=0; i < arlAgencyList.size(); i++) {
						     agency = (AgencyDao) arlAgencyList.get(i);
			   			     if (user.getAgencyCode().equalsIgnoreCase(agency.getAgencyCode()) && user.getCountryCode().equalsIgnoreCase(agency.getCountryCode())){
				   			     selected="selected";
							 }
	   	 		      %>

    	            		             <option value="<%=agency.getAgencyCode()+"_"+agency.getCountryCode()%>" <%=selected%>> <%=agency.getAgencyDesc()%></option>
					  <%
								selected="";
							 }
						  %>
				   </select>
				   <input type="submit" name="subSearch" value="Get Users" onClick="getUsers()">

				</td>
			</tr>

		</table>
		<BR><BR>
		<table align="center" class="tabularData" width=70% border="0" rules="cols">
			<thead class="th1">
				<tr>
					<th colspan="6">
						<div class="sectionTableTitle">
							<h2>List of Users:</h2>
						</div>
					</th>
				</tr>
			</thead>

			<tr class="headerD" style="border: thin solid">
				<td width="15%" class="center_note_left">User ID</td>
				<td width="20%" class="center_note_left">User Name</td>
				<td width="15%" class="center_note_left">Role</td>
				<td width="10%" class="center_note_left">Enabled</td>
				<td width="10%" class="center_note_left">Locked</td>
				<td width="10%" class="center_note_left">Status</td>
			</tr>
<%		if(size > 0){

				String rowClass="";
				String createStatus="";
				for(int i=0;i<size;i++){

					if (i%2 == 0) {
						rowClass = "even";
					} else {
						rowClass = "alt";
					}
					userD  = (UserDao) users.get(i);

					// User enabled flag
					String userEnabled = userD.getUserEnabled();
					////System.out.println("serachUser.jsp Start 05" + userD.getUserLogin());

					if (userEnabled.equals(Constants.DB_YES)) {
						userEnabled = "Yes";
					} else if (userEnabled.equals(Constants.DB_NO)) {
						userEnabled = "No";
					} else if (userEnabled.equals(Constants.DB_FLAG_DORMANT)) {
						userEnabled = "Dormant";
					}

					// create status
					if (userD.getCreateStatus().equals(Constants.DB_USER_CREATE_NEW)) {
						createStatus = "New";
					} else if (userD.getCreateStatus().equals(Constants.DB_USER_CREATE_APPROVE)) {
						createStatus = "Approved";
					}

%>
					<tr class ="<%=rowClass%>">
						<td width="15%" class="center_note_left"><%=userD.getUserLogin()%></td>
						<td width="20%" class="center_note_left"><%=userD.getUserName()%></td>
						<td width="15%" class="center_note_left"><%=userD.getRoleName()%></td>
						<td width="10%" class="center_note_left"><%= userEnabled%></td>
						<td width="10%" class="center_note_left"><%= userD.getUserLocked() ? "Yes" : "No" %></td>
						<td width="10%" class="center_note_left"><%=createStatus%></td>
					</tr>
<%
			}//end for users list
		}//end size > 0
%>
		</table>

	</form>

</body>

<%@ include file="../bottom.jsp" %>


